package com.kehutong.message.urlConfig;

import org.coraframework.http.HttpClient;
import org.coraframework.inject.Singleton;
import org.coraframework.json.JSON;
import org.coraframework.json.JSONObject;
import org.coraframework.util.CallBack;

import com.kehutong.common.urlconfig.UrlConfig;

/**
 * 企业微信相关url配置
 */
@Singleton
public class WebChatUrlConfig extends UrlConfig {
    protected WebChatUrlConfig() {
        super("tencent", "webchat_url");
    }

    /**
     * post请求,返回json
     * @param page
     * @param client
     * @return
     * @throws Exception
     */
    public JSONObject post(String page, CallBack<HttpClient> client) throws Exception {
        final HttpClient httpClient = HttpClient.create(getUrl() + page);
        client.call(httpClient);

        String text = httpClient.post();
        if (httpClient.getResponseCode() != 200) {
            return httpError(httpClient.getResponseCode(), "http状态错误");
        }

        return JSON.parseObject(text);
    }
}
